/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#include "playerpiano.hh"
#include <set>
#include <string>

namespace 
{
  using namespace playerpiano;

  void
  print_tau (const float* tau,
             unsigned int n_labels,
             unsigned int n_dims)
    {
      NominalEmbed::Taufunc<const float*, float> taufunc (tau, n_dims, n_labels);
      std::cout << std::fixed;
      std::cout << "tau = { ";

      for (unsigned int k = 0; k < n_labels; ++k)
        { 
          std::cout << ((k == 0) ? "{ " : ", { ");
    
          for (unsigned int j = 0; j < n_dims; ++j)
            { 
              std::cout << ((j == 0) ? "" : ", ") << taufunc (k, j);
            }

          std::cout << " }";
        }

      std::cout << " };\n";
      std::cout << std::scientific;
    }

  void
  print_alpha (const float* alpha,
               unsigned int n_workers,
               unsigned int n_labels,
               unsigned int n_dims)
    {
      bool first = true;
      float zero[n_labels * n_dims];

      memset (zero, 0, sizeof (zero));

      std::cout << std::fixed;
      std::cout << "alpha = { ";

      for (unsigned int w = 0; w < n_workers; ++w)
        {
          const float* alphaptr = alpha + w * n_labels * n_dims;

          if (memcmp (alphaptr, zero, sizeof (zero)) != 0)
            {
              std::cout << ((first) ? "{ " : ", { ") << w << ", { ";

              first = false;

              for (unsigned int k = 0; k < n_labels; ++k)
                {
                  std::cout << ((k == 0) ? "{ " : ", { ");

                  for (unsigned int j = 0; j < n_dims; ++j)
                    {
                      std::cout << ((j == 0) ? "" : ", ")
                                << alphaptr[k * n_dims + j];
                    }

                  std::cout << " }";
                }

              std::cout << " } }";
            }
        }

      std::cout << " };\n";
      std::cout << std::scientific;
    }
}

int
main (int   argc,
      char* argv[])
{
  std::set<std::string> takes_arg;

  takes_arg.insert ("--model");

  std::map<std::string, const char*> args = playerpiano::cl_parse (argc,
                                                                   argv,
                                                                   takes_arg);

  if (args.find ("-h") != args.end () ||
      args.find ("--help") != args.end () ||
      argc == 1)
    {
      std::cerr << "usage: extractembed --model modelfile" << std::endl;
      return 1;
    }

  args["--test_only"] = "1";

  Model* model = get_model (args);

  if (! model)
    {
      std::cerr << "ERROR: can't open model file '"
                << args["--model"]
                << "'"
                << std::endl;
      return 1;
    }

  if (strcmp (model->lookup<char*> ("--strategy").first,
              "nominalembed") == 0)
    {
      NominalEmbed nominal_embed (model);

      print_tau (model->lookup<float*> ("tau").first,
                 strtoul (model->lookup<char*> ("--n_labels").first, 0, 0),
                 strtoul (model->lookup<char*> ("--n_dims").first, 0, 0));

      print_alpha (model->lookup<float*> ("alpha").first,
                   (1U << strtoul (model->lookup<char*> ("--n_worker_bits").first, 0, 0)),
                   strtoul (model->lookup<char*> ("--n_labels").first, 0, 0),
                   strtoul (model->lookup<char*> ("--n_dims").first, 0, 0));
    }
  else
    {
      std::cerr << "ERROR: model file is for strategy '"
                << model->lookup<char*> ("--strategy").first
                << "' ; expected 'nominalembed'"
                << std::endl;
    }

  delete model;

  return 0;
}
